// pages/cart/cart.js
Page({
  data: {
    isLogin: false,
    isLoginShow: false,
    cartTotal: 0,
    cartList: [],
    cartIds: [],
    amountNumber: 0,
    cartStatus: 'buy'
  },
  onShow() {
    this.onRefreshPage()
  },
  async onRefreshPage() {
    await this.setData({
      isLogin: getApp().globalData.isLogin
    })
    if(this.data.isLogin === false) {
      this.setData({
        isLoginShow: false //等会记得修改
      })
    }
    this.getCartTotal()
    this.getCartList()
  },
  // 未登录弹框
  loginCancel() {
    this.setData({isLoginShow: false})
  },
  loginConfirm() {
    this.setData({isLoginShow: false})
    wx.navigateTo({
      url: '../../user_pkg/pages/login/login'
    })
  },
  // 获取购物车商品总数量
  async getCartTotal() {
    const res = await wx.http.get('/cart/total')
    const cartTotal = res.data.data.cartTotal
    this.setData({cartTotal})
  },
  // 获取购物车商品列表
  async getCartList() {
    const res = await wx.http.get('/cart/list')
    const cartList = res.data.data.list
    this.setData({cartList})
  },
  // 购物车商品数量修改
  async cartTotalAdd(ev) {
    const res = await wx.http.post('/cart/update', {
      goodsId: ev.mark.goodsId,
      goodsNum: ev.mark.goodsNum + 1,
      goodsSkuId: ev.mark.goodsSkuId
    })
    if(res.data.status !== 200) {
      wx.showToast({
        title: res.data.message,
        icon: 'error',
        duration: 2000
      })
    } else {
      this.getCartList()
      this.getCartTotal()
    }
  },
  async cartTotalReduce(ev) {
    const res = await wx.http.post('/cart/update', {
      goodsId: ev.mark.goodsId,
      goodsNum: ev.mark.goodsNum - 1,
      goodsSkuId: ev.mark.goodsSkuId
    })
    if(res.data.status !== 200) {
      wx.showToast({
        title: res.data.message,
        icon: 'error',
        duration: 2000
      })
    } else {
      this.getCartList()
      this.getCartTotal()
    }
  },
  // 复选框选中状态
  checkboxChange(e) {
    const cartId = e.mark.cartId
    let cartIds = this.data.cartIds
    let cartList = this.data.cartList
    const cartIndex = this.data.cartList.findIndex(item => item.id === cartId)
    if(!cartIds.includes(cartId)) {
      cartIds.push(cartId)
      cartList[cartIndex].checked = true
      this.setData({cartIds, cartList})
    } else {
      const index = cartIds.indexOf(cartId)
      cartIds.splice(index, 1)
      cartList[cartIndex].checked = false
      this.setData({cartIds, cartList})
    }
    this.getAmountNumber()
  },
  // 全选状态
  checkboxAllChange() {
    let cartList = this.data.cartList
    if(this.data.cartIds.length !== this.data.cartList.length) {
      const arr = this.data.cartList.map(item => item.id)
      cartList.forEach(({id, checked}) => {
        cartList[cartList.findIndex(item => item.id === id)].checked = true
      })
      this.setData({cartIds: arr, cartList})
    } else {
      cartList.forEach(({id, checked}) => {
        cartList[cartList.findIndex(item => item.id === id)].checked = false
      })
      this.setData({cartIds: [], cartList})
    }
    this.getAmountNumber()
  },
  inArray(id, arr) {
    if(arr.includes(id)) {
      return true
    } else {
      return false
    }
  },
  // 计算合计金额
  getAmountNumber() {
    const cartList = this.data.cartList
    const amountNumber = cartList.reduce((acc, item) => {
      const price = +item.goods.goods_price_max
      if(item.checked) {
        return acc + price
      }
      return acc
    }, 0)
    this.setData({amountNumber})
  },
  // 切换编辑/结算状态
  changeCartStatus() {
    if(this.data.cartStatus === 'buy') {
      this.setData({
        cartStatus: 'edit'
      })
    } else {
      this.setData({
        cartStatus: 'buy'
      })
    }
  },
  // 购物车编辑/结算
  async clickButton(ev) {
    // 购物车编辑
    if(this.data.cartStatus === 'edit') {
      const res = await wx.http.post('/cart/clear',{
        cartIds: this.data.cartIds
      })
      if(res.data.status === 200) {
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 2000
        })
        this.onRefreshPage()
        this.setData({amountNumber: 0, cartIds: []})
      } else {
        wx.showToast({
          title: '删除失败，请稍后再试',
          icon: 'error',
          duration: 2000
        })
      }
    }
    // 购物车结算
    if(this.data.cartStatus === 'buy') {
      const cartIds = ev.mark.cartIds.join()
      const mode = ev.mark.mode
      wx.navigateTo({
        url: `../../order_pkg/pages/settlement/settlement?cartIds=${cartIds}&mode=${mode}`
      })
    }
  }
})